package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.ProcessInfo;
import com.baomidou.ant.entity.SysDept;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.mapper.ProcessInfoMapper;
import com.baomidou.ant.service.IProcessInfoService;
import com.baomidou.ant.service.ISysDeptService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.RequestDtoVo;
import com.baomidou.ant.vo.request.RequestListProcessVo;
import com.baomidou.ant.vo.response.ResponseListProcessVo;
import com.baomidou.ant.vo.response.ResponseProcessNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@Service
public class ProcessInfoServiceImpl extends ServiceImpl<ProcessInfoMapper, ProcessInfo> implements IProcessInfoService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IProcessInfoService processInfoService;
    @Autowired
    private ISysDeptService sysDeptService;


    @Override
    public List<ResponseProcessNameVo> getProcess(RequestDtoVo requestDtoVo) {
        List<ResponseProcessNameVo> processNameList=new ArrayList<>();
            QueryWrapper<ProcessInfo> queryWrapper=new QueryWrapper<>();
            SysDept sysDept=sysDeptService.getById(requestDtoVo.getDeptId());
            queryWrapper.eq("process_dept",sysDept.getId());
            List<ProcessInfo> processInfoList=processInfoService.list(queryWrapper);
            for(ProcessInfo processInfo:processInfoList){
                ResponseProcessNameVo vo=new ResponseProcessNameVo();
                BeanUtils.copyProperties(processInfo,vo);
                processNameList.add(vo);
            }

        return processNameList;
    }

    @Override
    public List<ResponseProcessNameVo> getProcessOfUserEdit(RequestDtoVo requestDtoVo) {
        List<ResponseProcessNameVo> processNameList=new ArrayList<>();
        QueryWrapper<ProcessInfo> queryWrapper=new QueryWrapper<>();
        SysUser user=userService.getById(requestDtoVo.getUserId());
        queryWrapper.eq("process_dept",user.getDeptId());
        List<ProcessInfo> processInfoList=processInfoService.list(queryWrapper);
        for(ProcessInfo processInfo:processInfoList){
            ResponseProcessNameVo vo=new ResponseProcessNameVo();
            BeanUtils.copyProperties(processInfo,vo);
            processNameList.add(vo);
        }
        return processNameList;
    }

    @Override
    public IPage<ResponseListProcessVo> listProcess(RequestListProcessVo requestListProcessVo) {
        IPage<ProcessInfo> processInfoIPage=new Page<>(requestListProcessVo.getDisplayStart(),requestListProcessVo.getDisplayLength());
        QueryWrapper<ProcessInfo> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(requestListProcessVo.getProcessName())){
            queryWrapper.like("process_name",requestListProcessVo.getProcessName());
        }
        //
        SysUser user=userService.getById(requestListProcessVo.getUserId());
        //判断是否为管理员
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(StringUtils.isNotBlank(requestListProcessVo.getProcessDept())){
                Integer deptId=sysDeptService.queryByDeptName(requestListProcessVo.getProcessDept());
                queryWrapper.eq("process_dept",deptId);
            }
        }
        //不是管理员,只能看到本部门的流程
        else{
            queryWrapper.eq("process_dept",user.getDeptId());
        }
        //
        if(StringUtils.isNotBlank(requestListProcessVo.getWorkshop())){
            queryWrapper.like("work_shop",requestListProcessVo.getWorkshop());
        }
        //得到结果集
        IPage<ProcessInfo> page= page(processInfoIPage,queryWrapper);
        return page.convert(processInfo -> {
            ResponseListProcessVo vo=new ResponseListProcessVo();
            BeanUtils.copyProperties(processInfo,vo);
            SysDept dept=sysDeptService.getById(processInfo.getProcessDept());
            vo.setProcessDeptName(dept.getDept());
            return vo;
        });
    }


}
